home *** CD-ROM | disk | FTP | other *** search
/ CU Amiga Super CD-ROM 23 / CU Amiga - Super CD-ROM 23 (June 1998).iso / CUCD / Programming / OUI / rcs / new.cc < prev    next >
Encoding:
C/C++ Source or Header  |  1998-04-08  |  6.5 KB  |  309 lines

  1. head    1.3;
  2. access;
  3. symbols;
  4. locks
  5.     dlorre:1.3; strict;
  6. comment    @// @;
  7.  
  8.  
  9. 1.3
  10. date    97.09.17.08.16.26;    author dlorre;    state Exp;
  11. branches;
  12. next    1.2;
  13.  
  14. 1.2
  15. date    97.07.14.04.21.14;    author dlorre;    state Exp;
  16. branches;
  17. next    1.1;
  18.  
  19. 1.1
  20. date    96.08.22.02.05.10;    author dlorre;    state Exp;
  21. branches;
  22. next    ;
  23.  
  24.  
  25. desc
  26. @Oui.lib -- Object User Interface
  27. Projet créé en 1994
  28. Auteur: Dominique Lorre
  29. @
  30.  
  31.  
  32. 1.3
  33. log
  34. @sem is private
  35. @
  36. text
  37. @// $Id: new.cc 1.2 1997/07/14 04:21:14 dlorre Exp dlorre $
  38. #include <exec/types.h>
  39. #include <exec/memory.h>
  40.  
  41. #include <dos/dos.h>
  42. #include <dos/dosextens.h>
  43.  
  44. #include <intuition/intuition.h>
  45.  
  46. #include <new.h>
  47. #include <string.h>
  48. #include <unistd.h>
  49.  
  50. #include <proto/exec.h>
  51. #include <proto/dos.h>
  52. #include <proto/intuition.h>
  53. #include <clib/alib_protos.h>
  54. #include <mydebug.h>
  55. #include <compiler.h>
  56.  
  57. static ULONG OxygeneSize = 20000 ;
  58. APTR chipOxygene ;
  59.  
  60. static SignalSemaphore* MemorySemaphore ;
  61.  
  62. static Process *myproc ;
  63.  
  64. APTR anyPool=NULL, chipPool=NULL ;
  65.  
  66. static EasyStruct memFail = {
  67.     sizeof (struct EasyStruct),
  68.     0,
  69.     (UBYTE *)"Memory Warning",
  70.     (UBYTE *)"The system is running into a low memory situation.\n"
  71.     "I have been unable to allocate %lu bytes.\n\n"
  72.     "%lu bytes of chip memory have been given back to the system.\n"
  73.     "I can wait until you free some more and I can reallocate it.\n"
  74.     "Or, if you select Cancel I will not take back those bytes.\n"
  75.     "and %s allocations might be rejected.",
  76.     (UBYTE *)"Wait|Cancel",
  77. };
  78.  
  79.  
  80. LONG SAVEDS outmem(size_t s)
  81. {
  82. BOOL reply ;
  83.  
  84.     GETA4 ;
  85.  
  86.     LibFreePooled(chipPool, chipOxygene, OxygeneSize) ;
  87.     chipOxygene = NULL ;
  88.     reply = (BOOL)EasyRequest((Window *)myproc->pr_WindowPtr, &memFail,
  89.         NULL, s, OxygeneSize, myproc->pr_Task.tc_Node.ln_Name) ;
  90.     if (reply == 1) {
  91.         while(!(chipOxygene = LibAllocPooled(chipPool, OxygeneSize))) {
  92.             Delay(200) ;
  93.         }
  94.  
  95.     }
  96.     return reply ;
  97. }
  98.  
  99. void *operator new(size_t s)
  100. {
  101. unsigned long *p ;
  102.  
  103.     ObtainSemaphore(MemorySemaphore) ;
  104.     s += 4 ;
  105.     while ((p = (unsigned long *)LibAllocPooled(anyPool, s)) == NULL) {
  106.         if (chipOxygene) {
  107.             if (!outmem(s)) {
  108.                 p = (unsigned long *)LibAllocPooled(anyPool, s) ;
  109.             }
  110.         }
  111.         else
  112.             return NULL ;
  113.     }
  114.     *p++ = s ;
  115.     ReleaseSemaphore(MemorySemaphore) ;
  116.     return (void *)p ;
  117. }
  118.  
  119. void operator delete(void *p)
  120. {
  121. unsigned long *l = (unsigned long *)(p) ;
  122.  
  123.     ObtainSemaphore(MemorySemaphore) ;
  124.     l-- ;
  125.     LibFreePooled(anyPool, l, *l) ;
  126.     ReleaseSemaphore(MemorySemaphore) ;
  127. }
  128.  
  129. extern "C" {
  130. int _STI_50_initpool()
  131. {
  132.    myproc = (Process *)FindTask(0);
  133.  
  134.     MemorySemaphore = (SignalSemaphore *)AllocMem(sizeof(SignalSemaphore), MEMF_CLEAR|MEMF_PUBLIC) ;
  135.     if (MemorySemaphore) {
  136.         InitSemaphore(MemorySemaphore);
  137.  
  138.         anyPool = LibCreatePool(MEMF_CLEAR|MEMF_PUBLIC, 10000, 5000) ;
  139.         if (anyPool) {
  140.             chipPool = LibCreatePool(MEMF_CLEAR|MEMF_CHIP, 20000, 10000) ;
  141.             if (!chipPool) {
  142.                 LibDeletePool(anyPool) ;
  143.                 anyPool = NULL ;
  144.                 FreeMem(MemorySemaphore, sizeof(SignalSemaphore)) ;
  145.                 MemorySemaphore = NULL ;
  146.             }
  147.             chipOxygene = LibAllocPooled(chipPool, OxygeneSize) ;
  148.             if (!chipOxygene) {
  149.                 LibDeletePool(anyPool) ;
  150.                 LibDeletePool(chipPool) ;
  151.                 anyPool = NULL ;
  152.                 chipPool = NULL ;
  153.                 FreeMem(MemorySemaphore, sizeof(SignalSemaphore)) ;
  154.                 MemorySemaphore = NULL ;
  155.             }
  156.         }
  157.         else {
  158.             FreeMem(MemorySemaphore, sizeof(SignalSemaphore)) ;
  159.             MemorySemaphore = NULL ;
  160.         }
  161.     }
  162. bye:
  163. #if defined( __GNUG__ )
  164.     if (!MemorySemaphore) exit(20) ;
  165. #endif
  166.     return (MemorySemaphore)?0:1 ;
  167. }
  168.  
  169.  
  170. void _STD_50_freepool()
  171. {
  172.     FreeMem(MemorySemaphore, sizeof(SignalSemaphore)) ;
  173.     if (anyPool) LibDeletePool(anyPool) ;
  174.     if (chipPool) LibDeletePool(chipPool) ;
  175. }
  176. }
  177.  
  178. #if defined( __GNUG__ )
  179. ADD2INIT(_STI_50_initpool, -50);
  180. ADD2EXIT(_STD_50_freepool, -50);
  181. #endif
  182. @
  183.  
  184.  
  185. 1.2
  186. log
  187. @semaphore for memory
  188. @
  189. text
  190. @d1 1
  191. a1 1
  192. // $Id$
  193. a24 1
  194. static char MemorySemName[50] ;
  195. d98 22
  196. a119 22
  197.    strcpy(MemorySemName, "OUI Mem SemaphoreXXXXXXXX") ;
  198.    mktemp(MemorySemName) ;
  199.  
  200. //   Forbid();
  201.     if (!FindSemaphore((UBYTE *)MemorySemName)) {
  202.         MemorySemaphore = (SignalSemaphore *)AllocMem(sizeof(SignalSemaphore), MEMF_CLEAR|MEMF_PUBLIC) ;
  203.         MemorySemaphore->ss_Link.ln_Pri = 0;
  204.         MemorySemaphore->ss_Link.ln_Name = MemorySemName ;
  205.         AddSemaphore(MemorySemaphore);
  206.     }
  207.     else {
  208. //        Permit();
  209.         goto bye ;
  210.     }
  211. //    Permit();
  212.  
  213.     anyPool = LibCreatePool(MEMF_CLEAR|MEMF_PUBLIC, 10000, 5000) ;
  214.     if (anyPool) {
  215.         chipPool = LibCreatePool(MEMF_CLEAR|MEMF_CHIP, 20000, 10000) ;
  216.         if (!chipPool) {
  217.             LibDeletePool(anyPool) ;
  218.             anyPool = NULL ;
  219. d121 3
  220. a123 6
  221.         chipOxygene = LibAllocPooled(chipPool, OxygeneSize) ;
  222.         if (!chipOxygene) {
  223.             LibDeletePool(anyPool) ;
  224.             LibDeletePool(chipPool) ;
  225.             anyPool = NULL ;
  226.             chipPool = NULL ;
  227. d128 1
  228. a128 1
  229.     if (!chipPool) exit(20) ;
  230. d130 1
  231. a130 1
  232.     return (chipPool)?0:1 ;
  233. a135 7
  234. //    Forbid();
  235.     if (MemorySemaphore = (SignalSemaphore *)
  236.     FindSemaphore((UBYTE *)MemorySemName)) {
  237.         RemSemaphore(MemorySemaphore);
  238.         ObtainSemaphore(MemorySemaphore);
  239.         ReleaseSemaphore(MemorySemaphore);
  240.     }
  241. a136 1
  242. //    Permit();
  243. @
  244.  
  245.  
  246. 1.1
  247. log
  248. @Initial revision
  249. @
  250. text
  251. @d1 1
  252. d9 2
  253. a10 1
  254. #include <stdio.h>
  255. d12 1
  256. d14 6
  257. a19 14
  258. #include "new.h"
  259.  
  260. #include <cxxproto/exec.h>
  261. #include <cxxproto/intuition.h>
  262.  
  263. extern "C" {
  264. APTR __stdargs LibAllocPooled( APTR poolHeader, unsigned long memSize );
  265. APTR __stdargs LibCreatePool( unsigned long memFlags, unsigned long puddleSize,
  266.         unsigned long threshSize );
  267. void __stdargs LibDeletePool( APTR poolHeader );
  268. void __stdargs LibFreePooled( APTR poolHeader, APTR memory, unsigned long memSize );
  269. void kprintf(STRPTR format, ...) ;
  270. }
  271.  
  272. d21 2
  273. d24 2
  274. d31 1
  275. a31 1
  276. EasyStruct memFail = {
  277. d34 8
  278. a41 3
  279.     "Memory Warning",
  280.     "Allocation of %lu bytes in memory failed.\nPlease free some memory and try again.",
  281.     "Retry|Cancel",
  282. d45 1
  283. a45 1
  284. BOOL __saveds outmem(size_t s)
  285. d49 12
  286. a60 1
  287.     reply = (BOOL)EasyRequest((Window *)myproc->pr_WindowPtr, &memFail, NULL, s) ;
  288. d68 1
  289. d71 6
  290. a76 1
  291.         if (!outmem(s))
  292. d80 1
  293. d86 1
  294. a86 1
  295. unsigned long *l = (unsigned long *)p ;
  296. d88 4
  297. a91 1
  298.     LibFreePooled(anyPool, --l, *l) ;
  299. d94 1
  300. d97 18
  301. a114 1
  302.     myproc = (Process *)FindTask(0);
  303. d122 7
  304. d130 4
  305. d140 9
  306. d152 1
  307. d154 4
  308. @
  309.